package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.order.OrderForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Reference(timeout = 3000, retries = 2, check = false)
    OrderCoreService orderCoreService;
    @Reference(timeout = 3000, retries = 2, check = false)
    OrderQueryService orderQueryService;

    @PostMapping("/order")
    public ResponseData orderAdd(@RequestBody OrderForm form, HttpServletRequest servletRequest) {
        // TODO 订单id
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        CreateOrderRequest request = new CreateOrderRequest();
        request.setUserId(uid);
        request.setAddressId(form.getAddressId());
        request.setTel(form.getTel());
        request.setUserName(form.getUserName());
        request.setStreetName(form.getStreetName());
        request.setCartProductDtoList(form.getCartProductDtoList());
        request.setOrderTotal(form.getOrderTotal());

        CreateOrderResponse response = orderCoreService.createOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("/order")
    public ResponseData orderList(HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        OrderListRequest request = new OrderListRequest();
        request.setUserId(uid);

        OrderListResponse response = orderQueryService.orderList(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable("id") Long id, HttpServletRequest servletRequest) {

        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(String.valueOf(id));

        OrderDetailResponse response = orderQueryService.orderDetail(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @PostMapping("/cancelOrder")
    public ResponseData orderCancel() {

//        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
//            return new ResponseUtil<>().setData(response);
//        }
//        return new ResponseUtil<>().setErrorMsg(response.getMsg());
        return null;
    }

    @DeleteMapping("/order/{id}")
    public ResponseData orderDelete(@PathVariable("id") Long id) {

//        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
//            return new ResponseUtil<>().setData(response);
//        }
//        return new ResponseUtil<>().setErrorMsg(response.getMsg());
        return null;
    }
}
